package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import androidx.activity.result.ActivityResultCallback;
import androidx.appcompat.app.AppCompatActivity;
import easier.app.x.CommonCode;
import easier.app.x.result.pic.BitmapCompressor;
import easier.app.x.result.pic.TakePhoto;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;
import java.io.File;

public class MainActivity68 extends AppCompatActivity {

    private static final String TAG = "MainActivity68";
    private ImageView image;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity68.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main68 );

        initView();
    }

    private void test() {

        TakePhoto.fromGallery( MainActivity68.this.getSupportFragmentManager(), new ActivityResultCallback< Uri >() {
            @Override
            public void onActivityResult( Uri result ) {

                Observable.just( "" )
                    .map( new Function< String, File >() {
                        @Override
                        public File apply( String s ) throws Throwable {

                            File file = CommonCode.copyFromUri( MainActivity68.this, result, "sefd", null );

                                    /*File compress = BitmapCompressor.compress( MainActivity68.this, file, 200 * 1024 );
                                    if ( compress != null ) {
                                        Log.d( TAG, "accept: " + compress + " " + compress.length() );
                                        compress.deleteOnExit();
                                    } else {
                                        Log.d( TAG, "accept: compress null" );
                                    }*/

                            File compress1 = BitmapCompressor.compress( MainActivity68.this, file, 1500, 1500 );
                            if ( compress1 != null ) {
                                Log.d( TAG, "accept: " + compress1 + " " + compress1.length() );
                                return compress1;
                            } else {
                                Log.d( TAG, "accept: compress1 null" );
                                return null;
                            }
                        }
                    } )
                    .subscribeOn( Schedulers.io() )
                    .observeOn( AndroidSchedulers.mainThread() )
                    .doOnNext( new Consumer< File >() {
                        @Override
                        public void accept( File file ) throws Throwable {

                            image.setImageURI( Uri.fromFile( file ) );
                        }
                    } )
                    .subscribe();
            }
        } );
    }

    private void initView() {

        image = (ImageView) findViewById( R.id.image );
        image.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                test();
            }
        } );
    }
}